package com.biye.api.controller.common;

import com.biye.api.service.ExcelTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Api( tags = "数据导入模板下载的对外接口" )
@Controller
@RequestMapping(value = "/common")
public class DownloadController {

    @Autowired
    private ExcelTemplateService excelTemplateService;

    @RequestMapping(value = "/infoTemplate", method = RequestMethod.GET) // 如果@ResponseBody返回String，则需要加produces = "text/html;charset=UTF-8"
    @ApiOperation(value = "师生信息导入模板下载", notes = "无须传参")
    public void downloadTemplate(HttpServletResponse response) throws UnsupportedEncodingException {
        //待下载文件名
        String fileName = "师生信息导入模板.xlsx";
        //设置响应头
        response.setContentType("application/octet-stream");
        response.setHeader("content-type", "application/octet-stream");
        try {
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName,"utf8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        excelTemplateService.downloadPersonInfoTemplate(response, fileName);
    }




}
